﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditAdministrator : AdminPage, IEditAdministratorView
    {
        private EditAdministratorPresenter _presenter;

        #region IEditAdministratorView Members

        public int EditedAdministratorID
        {
            get
            {
                if (ViewState["ID"] == null)
                    ViewState["ID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["ID"];
            }
        }

        public void SetEditedAdministratorID(int id)
        {
            ViewState["ID"] = id;
        }

        public string Username
        {
            get { return txtUsername.Text; }
            set { txtUsername.Text = value; }
        }

        public string Password
        {
            get
            {
                return txtPassword.Text == hidPassword.Value ? Utilities.Cipher.Decrypt(txtPassword.Text) : txtPassword.Text;
            }
            set
            {
                string encryptedPassword = Utilities.Cipher.Encrypt(value);
                txtPassword.Attributes.Add("value", encryptedPassword);
                hidPassword.Value = encryptedPassword;
            }
        }

        public string Email
        {
            get { return txtEmail.Text; }
            set { txtEmail.Text = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditAdministrator.xml"; }
        }

        public void SetAdministratorGroups(IList<AdministratorGroup> groups)
        {
            foreach (AdministratorGroup group in groups)
                ddlAdministratorGroups.Items.Add(new ListItem(group.Name, group.ID.ToString()));
        }

        public int SelectedAdministratorGroupID
        {
            get { return int.Parse(ddlAdministratorGroups.SelectedValue); }
            set { ddlAdministratorGroups.SelectedValue = value.ToString(); }
        }

        public string Firstname
        {
            get { return txtFirstname.Text; }
            set { txtFirstname.Text = value; }
        }

        public string Lastname
        {
            get { return txtLastname.Text; }
            set { txtLastname.Text = value; }
        }

        public void SetDefaultSites(IList<Site> sites)
        {
            string previousSelectedValue = ddlDefaultSites.SelectedValue;
            ddlDefaultSites.Items.Clear();
            foreach (Site s in sites)
                ddlDefaultSites.Items.Add(new ListItem(s.Domain, s.ID.ToString()));
            if (ddlDefaultSites.Items.Count > 0)
                trDefaultSite.Visible = true;
            else
            {
                trDefaultSite.Visible = false;
                ddlDefaultSites.Items.Add(new ListItem("-1", "-1"));
            }
            ddlDefaultSites.SelectedValue = previousSelectedValue;
            upnlProperties.Update();
        }

        public int SelectedDefaultSiteID
        {
            get { return int.Parse(ddlDefaultSites.SelectedValue); }
            set { ddlDefaultSites.SelectedValue = value.ToString(); }
        }

        public void SetSystemLocales(IList<string> systemLocales)
        {
            foreach (string locale in systemLocales)
                ddlSystemLocales.Items.Add(new ListItem(locale, locale));
        }

        public string DefaultSystemLocale
        {
            get { return ddlSystemLocales.SelectedValue; }
            set { ddlSystemLocales.SelectedValue = value; }
        }

        public string DateFormat
        {
            get { return txtDateFormat.Text; }
            set { txtDateFormat.Text = value; }
        }

        public string TimeFormat
        {
            get { return txtTimeFormat.Text; }
            set { txtTimeFormat.Text = value; }
        }

        public DayOfWeek? FirstDayOfWeek
        {
            get
            {
                if (ddlDaysOfWeek.SelectedIndex > 0)
                {
                    return (DayOfWeek)Enum.Parse(typeof(DayOfWeek), ddlDaysOfWeek.SelectedValue);
                }
                return null;
            }
            set
            {
                ddlDaysOfWeek.SelectedValue = value.ToString();
            }
        }
        public bool ShowIntroHelp
        {
            get { return chkShowIntroHelp.Checked; }
            set { chkShowIntroHelp.Checked = value; }
        }

        public void SetAvailableSites(IList<Site> sites)
        {
            rpAvailableSites.DataSource = sites;
            rpAvailableSites.DataBind();
            trAvailableSites.Visible = sites.Count > 0;
        }

        public IEnumerable<int> GetSelectedAvailableSiteIDs()
        {
            return (from RepeaterItem ri in rpAvailableSites.Items
                    select (CheckBox) ri.FindControl("chkSite")
                    into chk
                    where chk.Checked
                    select int.Parse(chk.Attributes["Value"])).ToList();
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditAdministratorPresenter(this);
            if (Page.IsPostBack)
                return;
            ddlDaysOfWeek.Items.Add(new ListItem("- " + GetText(this, "llSelect") + " -", "-1"));
            ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llMonday"), DayOfWeek.Monday.ToString()));
            //ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llTuesday"), DayOfWeek.Tuesday.ToString()));
            //ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llWednesday"), DayOfWeek.Wednesday.ToString()));
            //ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llThursday"), DayOfWeek.Thursday.ToString()));
            //ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llFriday"), DayOfWeek.Friday.ToString()));
            //ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llSaturday"), DayOfWeek.Saturday.ToString()));
            ddlDaysOfWeek.Items.Add(new ListItem(GetText(this, "llSunday"), DayOfWeek.Sunday.ToString()));
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            txtUsername.Focus();
            ddlAdministratorGroups.Attributes.Add("onchange", "updateAvailableSitesWhenAdminGroupChanges();");
        }

        protected void rpAvailableSites_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var site = (Site) e.Item.DataItem;
            var chkSite = (CheckBox) e.Item.FindControl("chkSite");
            var litSite = (Literal) e.Item.FindControl("litSite");
            chkSite.Attributes.Add("value", site.ID.ToString());
            chkSite.Checked = _presenter.HasAccessTo(site);
            chkSite.Enabled = SelectedAdministratorGroupID != 1;
            litSite.Text = site.Domain;
        }

        protected void chkSite_Changed(object sender, EventArgs e)
        {
            _presenter.SetDefaultSites();
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("Administrators.aspx");
        }
    }
}